﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class RelatedProductRepository : Data.Repository.IRelatedProductRepository
    {
        private LinqToECommerceDBDataContext _dbContext;
        
        #region IRelatedProductRepository Members

        public int CreateNewRelatedProduct(Data.Entity.RelatedProduct theNewRelatedProduct)
        {
            if (theNewRelatedProduct == null)
            {
                return 0;
            }

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_RelatedProduct_Create(theNewRelatedProduct.ProductID1, theNewRelatedProduct.ProductID2, theNewRelatedProduct.DisplayOrder);
			}

			return result;
		}

		/// <summary>
		/// delete related product method
		/// </summary>
		/// <param name="productID1"></param>
		/// <param name="productID2"></param>
		/// <returns>0: failed 1: successed</returns>
		public int DeleteRelatedProduct(int productID1, int productID2)
		{
			if(productID1 <= 0 || productID2 <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_RelatedProduct_Delete(productID1, productID2);
			}

			return result;
		}

		public IList<Data.Entity.RelatedProduct> GetRelatedProducts(int productID)
		{
			if(productID <= 0)
			{
				return null;
			}

			List<ECommerce_GetRelatedProductsResult> lstResult;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetRelatedProducts(productID).OrderBy(exp => exp.DisplayOrder);

				lstResult = data.ToList();
			}

			if(lstResult != null && lstResult.Count > 0)
			{
				var result = new List<Data.Entity.RelatedProduct>();

				foreach(var rp in lstResult)
				{
					var theEntity = new Data.Entity.RelatedProduct()
					{
						DisplayOrder = rp.DisplayOrder,
						ProductID1 = rp.ProductID1,
						ProductID2 = rp.ProductID2,
						RelatedProductID = rp.RelatedProductID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int UpdateRelatedProduct(Data.Entity.RelatedProduct theRelatedProduct)
		{
			if(theRelatedProduct == null || theRelatedProduct.RelatedProductID <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbRelatedProduct = (from rp in _dbContext.RelatedProducts
										where rp.RelatedProductID == theRelatedProduct.RelatedProductID
										select rp).FirstOrDefault();

				if(dbRelatedProduct == null)
				{
					result = 0;
				}
				else
				{
					dbRelatedProduct.DisplayOrder = theRelatedProduct.DisplayOrder;
					dbRelatedProduct.ProductID1 = theRelatedProduct.ProductID1;
					dbRelatedProduct.ProductID2 = theRelatedProduct.ProductID2;

					_dbContext.SubmitChanges();

					result = 1;
				}
			}

			return result;
		}

		public IList<Data.Entity.Product> GetRelatedProducts(System.Collections.Generic.IEnumerable<int> prodIDs)
		{
			if(prodIDs == null || prodIDs.Count() <= 0)
			{
				return null;
			}

			var lstData = new List<Data.Product>();

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = from p in _dbContext.Products
						   where prodIDs.Contains(p.ProductID) && !p.Deleted && p.Published
						   select p;

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Product>();

				foreach(var p in lstData)
				{
					var theEntity = new Data.Entity.Product()
					{
						AllowUserRatings = p.AllowUserRatings,
						AllowUserReviews = p.AllowUserReviews,
						Cost = p.Cost,
						CreatedOn = p.CreatedOn,
						Deleted = p.Deleted,
						FullDescription = p.FullDescription,
						ListPrice = p.ListPrice,
						MetaKeywords = p.MetaKeywords,
						MetaTitle = p.MetaTitle,
						Name = p.Name,
						ProductID = p.ProductID,
						Published = p.Published,
						Quantity = p.Quantity,
						RatingSum = p.RatingSum,
						ShortDescription = p.ShortDescription,
						ShowOnHomePage = p.ShowOnHomePage,
						Size = p.Size,
						SizeUnit = p.SizeUnit,
						TotalRatingVotes = p.TotalRatingVotes,
						UpdatedOn = p.UpdatedOn,
						Weight = p.Weight,
						WeightUnit = p.WeightUnit
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		#endregion
	}
}
